<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
      
        
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="名称">
          <a-input placeholder="请输入名称" v-decorator="['RegionName', validatorRules.regionname]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="编码">
          <a-input placeholder="请输入编码" v-decorator="['RegionCode', {}]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="类型">
          <a-select @change="changeSelect" placeholder="请选择类型" v-decorator="['RegionType', {}]" disabled>
            <a-select-option value="">请选择</a-select-option>
            <a-select-option value="1">省</a-select-option>
            <a-select-option value="2">市</a-select-option>
            <a-select-option value="3">区县</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="上级">
          <a-select placeholder="请输入上级" v-decorator="['ParentId', {}]" >
            <a-select-option value="">请选择</a-select-option>
            <a-select-option :value="item.RegionId" v-for="(item,index) in parentArr">{{item.RegionName}}</a-select-option>
          </a-select>
        </a-form-item>
		
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"

  export default {
    name: "BaseRegionModal",
    data () {
      return {
        title:"操作",
        parentArr:[],
        visible: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        regionid:{rules: [{ required: true, message: '请输入区域!' }]},
        parentid:{rules: [{ required: true, message: '请输入上级!' }]},
        regionname:{rules: [{ required: true, message: '请输入名称!' }]},
        regiontype:{rules: [{ required: true, message: '请输入类型!' }]},
        updatebyid:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        agencyid:{rules: [{ required: true, message: '请输入agencyId!' }]},
        regionsn:{rules: [{ required: true, message: '请输入SN!' }]},
        sjw:{rules: [{ required: true, message: '请输入sjw!' }]},
        regioncode:{rules: [{ required: true, message: '请输入代码!' }]},
        parentcode:{rules: [{ required: true, message: '请输入parentCode!' }]},
        updateby:{rules: [{ required: true, message: '请输入最近更新人!' }]},
        ts:{rules: [{ required: true, message: '请输入最近更新日期!' }]},
        datastatus:{rules: [{ required: true, message: '请输入状态!' }]},
        },
        RegionType:'',
        url: {
          add: "/baseRegion/add",
          edit: "/baseRegion/update",
        },
      }
    },
    created () {
    },
    methods: {
      setRegionType(type){
        this.RegionType = type;
      },
      changeSelect(type){
        this.$nextTick(()=>{
          let data = this.form.getFieldsValue();
          this.parentArr = [];
          if(type !== 'init'){
            this.form.setFieldsValue({
              ParentId:''
            })
          }
          
          let regionType = data.RegionType;
          if(!regionType){
            return false;
          }
          regionType = parseInt(regionType)-1;
          this.$http.postAction("/baseRegion/queryRows",{pageIndex:1,pageSize:9999,selectFields:[],conditions:[{
              "field":"RegionType",
              "operator":"=",
              "value":regionType
            }]}).then(res=>{
            if(res.status == 1){
              this.parentArr = res.data.rows;
            }
          })
        })
      },
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.model.RegionType = this.RegionType;
        this.visible = true;
        this.$nextTick(() => {
          this.model.ParentId = this.model.Parent;
          this.form.setFieldsValue(pick(this.model,'RegionName','RegionType','RegionCode','ParentId'))
    		  //时间格式化
          if(this.model.RegionId){
            this.changeSelect('init');
          }else{
            this.changeSelect();
          }
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.RegionId){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            
            console.log(formData)
            this.$http.postAction(httpurl,formData).then((res)=>{
              if(res.status == 1){
                that.$message.success('提交成功');
                if(method == 'post'){
                  this.form.resetFields();
                  this.model = {};
                }else{
                  that.close();
                }
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              // that.close();
            })



          }
        })
      },
      handleCancel () {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>